#include "playscene.h"


PlayScene::PlayScene(int lv, QWidget *parent) : MyMainWindow(parent)
{
    //初始化状态
    this->hasWin = false;
    //按钮
    backButton = new MyButton(":/res/BackButton.png", ":/res/BackButtonSelected.png", this);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());
    backButton->resize(72, 32);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());

    //游戏等级文本
    lvLable = new QLabel(QString("Level :%1").arg(lv), this);
    lvLable->setFont(QFont("华文新魏",20));
    lvLable->resize(150, 50);
    lvLable->move(30, this->height() - lvLable->height());

    const int colWidth = 50;
    const int rowHeight = 50;
    const int xOffset = 57;
    const int yOffset = 200;

    //导入数据
    data = new dataConfig(this);
    QVector<QVector<int>> coin = data->mData[lv];
    //十六个硬币组成棋盘
    for(int row = 0; row < coin.size(); row++)
    {
        coinBtn.push_back(QVector<CoinButton*>());
        for(int col = 0; col < coin[row].size(); col++)
        {
            CoinButton *temp = new CoinButton(this);
            this->coinBtn[row].push_back(temp);
            int x = col * colWidth + xOffset;
            int y = row * rowHeight + yOffset;
            temp->setGeometry(x ,y, 50, 50);
            temp->setStat(coin[row][col]);

            //点击按钮，改变金币
            connect(temp, &CoinButton::clicked, [=](){
                this->filp(row, col);
            });
        }
    }

    //绑定返回事件
    connect(backButton, &MyButton::clicked, this, &PlayScene::backButtonSig);
}

void PlayScene::filp(int x, int y)
{
    if(this->hasWin){
        return;
    }
    //音效
    SoundUtil s;
    s.coinSound();
    this->coinBtn[x][y]->filp();
    //设置延时
    QTimer::singleShot(250,[=](){
        //上下左右
        if(x - 1 >= 0){
            this->coinBtn[x - 1][y]->filp();
        }
        if(x + 1 < coinBtn[0].size()){
            this->coinBtn[x + 1][y]->filp();
        }
        if(y - 1 >= 0){
            this->coinBtn[x][y - 1]->filp();
        }
        if(y + 1 < coinBtn[0].size()){
            this->coinBtn[x][y + 1]->filp();
        }

        QTimer::singleShot(160, [=]{
            this->judgeWin();
        });
    });
}

void PlayScene::judgeWin()
{
    //循环判断，如果有银币就退出方法
    for(int i = 0; i < this->coinBtn.size(); i++)
    {
        for(int j = 0; j < this->coinBtn[i].size(); j++)
        {
            if(this->coinBtn[i][j]->getStat() == 0)
            {
                return;
            }
        }
    }

    //显示胜利图片
    QLabel *lableWin = new QLabel(this);
    QPixmap pm(":/res/LevelCompletedDialogBg.png");
    lableWin->setPixmap(pm);
    lableWin->resize(pm.size());
    lableWin->move(this->width() / 2 - lableWin->width() / 2, -lableWin->height());
    lableWin->show();
    //设置动画
    QPropertyAnimation *animation = new QPropertyAnimation(lableWin, "geometry", this);
    //设置起始位置
    animation->setStartValue(lableWin->geometry());
    //设置终点
    animation->setEndValue(QRect(lableWin->x(), lableWin->y() + 170, lableWin->width(), lableWin->height()));
    //设置时间
    animation->setDuration(1000);
    //设置运动曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //设置运动完就释放内存
    animation->start(QAbstractAnimation::DeleteWhenStopped);

    //音效
    SoundUtil s;
    s.winSound();

    //胜利状态
    this->hasWin = true;

}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //移动画家位置
    painter.translate(0, this->menuBar()->height());
    QPixmap pm(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pm);
    pm.load(":/res/Title.png");
    pm = pm.scaled(pm.width() / 2, pm.height() / 2);
    painter.drawPixmap(10, 0, pm);
}
